package com.ihome.android.market2.view;


import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

import com.ihome.android.academy.R;

public class DialogFactory {
	private final String	TAG	= DialogFactory.class.getSimpleName();
	private Context			context;
	private LayoutInflater	inflater;
	private Toast			toast;
	private Dialog			dialog;

	public DialogFactory(Context context) {
		this.context = context;
		toast = new Toast(context);
		this.inflater = LayoutInflater.from(context);
	}

	/**
	 * Make a standard toast that just contains a text view.
	 * 
	 * @param text
	 *            The text to show. Can be formatted text.
	 * @param duration
	 *            How long to display the message. Either
	 *            {@link Toast#LENGTH_SHORT} or {@link Toast#LENGTH_LONG}
	 * 
	 */
	public void showToast(String text, int duration) {
//		View layout = LayoutInflater.from(context.getApplicationContext()).inflate(R.layout.toast, null);
//		TextView tv = (TextView) layout.findViewById(R.id.toast_textview);
//		Button btn = (Button) layout.findViewById(R.id.bt_toast_close);
//		tv.setText(text);
//		tv.setLayoutParams(new LinearLayout.LayoutParams(Util.getWindowWidth(context) - (int) (40 * Util.getWindowDensity(context)), LinearLayout.LayoutParams.WRAP_CONTENT));
//		btn.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				toast.cancel();
//			}
//		});
//		btn.setLayoutParams(new LinearLayout.LayoutParams((int) (40 * Util.getWindowDensity(context)), LinearLayout.LayoutParams.WRAP_CONTENT));
//		toast.setGravity(Gravity.BOTTOM, 0, 0);
//		toast.setDuration(duration);
//		toast.setView(layout);
//		toast.setMargin(0, 0);
//		toast.show();
		
		View toastRoot = LayoutInflater.from(context.getApplicationContext()).inflate(R.layout.detail_toast, null);
		TextView message = (TextView) toastRoot.findViewById(R.id.message);

		message.setText(text);
		Toast toastStart = new Toast(context);
		toastStart.setGravity(Gravity.CENTER, 0, 200);
		toastStart.setDuration(duration);
		toastStart.setView(toastRoot);
		toastStart.show();
	}

	/**
	 * 一个按钮
	 * 
	 * @param title
	 *            标题
	 * @param content
	 *            提示内容
	 * @param okContent
	 *            dialog左边按钮内容
	 * @param onClickListener
	 * @param isCancelable
	 */
	public void showDialog(String title, String content, String okContent, final onBtnClickListener onClickListener, boolean isCancelable) {
		showDialog3Btn(title, content, okContent, null, null, onClickListener, isCancelable);
	}

	public void showDialog(String title, String content, String okContent, final onBtnClickListener onClickListener) {
		showDialog(title, content, okContent, onClickListener, false);
	}

	public void showDialog(String content) {
		showDialog(null, content, null, null);
	}

	public void showDialog(String title, String content) {
		showDialog(title, content, null, null);
	}

	/**
	 * 2个按钮
	 * 
	 * @param title
	 *            标题
	 * @param content
	 *            提示内容
	 * @param ok
	 *            dialog左边按钮内容
	 * @param cancle
	 *            dialog右边按钮内容
	 * @param onClickListener
	 *            点击事件
	 * @param isCancelable
	 *            能否被返回键取消
	 */
	public void showDialog2Btn(String title, String content, String ok, String cancle, final onBtnClickListener onClickListener, boolean isCancelable) {
		showDialog3Btn(title, content, ok, null, cancle, onClickListener, isCancelable);
	}

	public void showDialog2Btn(String title, String content, String ok, String cancle, final onBtnClickListener onClickListener) {
		this.showDialog2Btn(title, content, ok, cancle, onClickListener, false);
	}

	/**
	 * 3个按钮
	 * 
	 * @param title
	 *            提示标题
	 * @param content
	 *            提示内容
	 * @param btn_left_content
	 *            dialog左边确定按钮文字
	 * @param btn_center_content
	 *            dialog中间自定义按钮文字
	 * @param btn_right_content
	 *            dialog右边取消按钮文字
	 * @param onClickListener
	 *            点击事件
	 * @param isCancelable
	 *            能否响应返回键
	 */
	public void showDialog3Btn(String title, String content, String btn_left_content, String btn_center_content, String btn_right_content, final onBtnClickListener onClickListener,
			boolean isCancelable) {
		dialog = new Dialog(context, R.style.dialog);
		dialog.setCancelable(isCancelable);
		dialog.setContentView(R.layout.dialog_factory_button);
		Button btn_left = (Button) dialog.findViewById(R.id.dialog_factory_bt_opt);// 左一

		Button btn_center = (Button) dialog.findViewById(R.id.dialog_factory_bt_ok);// 中间
		btn_center.setVisibility(View.VISIBLE);
		Button btn_right = (Button) dialog.findViewById(R.id.dialog_factory_bt_cancle);// 右一
		btn_right.setVisibility(View.VISIBLE);

		TextView tvContent = (TextView) dialog.findViewById(R.id.dialog_factory_tv_content);
		tvContent.setMovementMethod(new ScrollingMovementMethod());
		TextView tvTitle = (TextView) dialog.findViewById(R.id.dialog_factory_tv_title);

		tvTitle.setText((null == title) ? "" : title);
		if (content.length() > 19) {
			tvContent.setTextSize(22);
		}
		if (tvContent != null) {
			tvContent.setText((null == content) ? "" : content);
		}
		if (btn_left_content != null) {
			btn_left.setText(TextUtils.isEmpty(btn_left_content) ? context.getString(R.string.ok) : btn_left_content);
		}

		if (btn_center_content != null) {
			btn_center.setText(btn_center_content);
		}
		else {
			btn_center.setVisibility(View.GONE);
		}
		if (btn_right_content != null) {
			btn_right.setText(btn_right_content);
		}
		else {
			btn_right.setVisibility(View.GONE);
		}
		if (btn_right_content == null && btn_center_content == null) {
			btn_left.setBackgroundResource(R.drawable.dialog_btn_long_bg);

			LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
			btn_left.setLayoutParams(params);
		}

		btn_left.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (onClickListener != null)
					onClickListener.btnLeftClickListener(v);
				dialog.dismiss();
			}
		});
		if (btn_center_content != null) {
			btn_center.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					if (onClickListener != null)
						onClickListener.btnNeutralClickListener(v);
					dialog.dismiss();
				}
			});
		}

		if (btn_right_content != null) {
			btn_right.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					if (onClickListener != null)
						onClickListener.btnRightClickListener(v);
					dialog.dismiss();
				}
			});
		}

		btn_left.requestFocus();
		dialog.show();
	}


	public interface onBtnClickListener {
		/**
		 * 左边确认按钮
		 * 
		 * @param v
		 */
		public abstract void btnLeftClickListener(View v);

		/**
		 * dialog中间自定义按钮
		 * 
		 * @param v
		 */
		public abstract void btnNeutralClickListener(View v);

		/**
		 * dialog最右边按钮
		 * 
		 * @param v
		 */
		public abstract void btnRightClickListener(View v);

		/**
		 * 右上角关闭
		 * 
		 * @param v
		 */
		public abstract void btnCloseClickListener(View v);
	}

}
